package com.example.okhttputils.mvp.ui.util;

import android.app.Activity;

import java.lang.ref.SoftReference;
import java.util.List;
import java.util.Stack;

/**
 * @author:created by njb
 * 时间:2019/3/8 15 25
 * 描述:
 */
public class ActivityManager {
    private static ActivityManager instance;
    private List<SoftReference<Activity>> activityStack;// activity栈

    // 单例模式
    public static ActivityManager getInstance() {
        if (instance == null) {
            instance = new ActivityManager();
        }

        return instance;
    }

    // 把一个activity压入栈中
    public void pushOneActivity(Activity actvity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(new SoftReference(actvity));
    }


    // 移除一个activity
    public void popOneActivity(Activity activity) {
        if (activity == null || activity.getClass() == null) {
            return;
        }


        if (activityStack != null && activityStack.size() > 0) {
            if (activity != null) {
                for (int i = 0; i < activityStack.size(); i++) {
                    if (activityStack.get(i) != null && activityStack.get(i).get() != null && activityStack.get(i).get().getClass() != null
                            && activity != null && activity.getClass() != null
                            && activityStack.get(i).get().getClass().getSimpleName().equals(activity.getClass().getSimpleName())) {
                        activityStack.remove(activityStack.get(i));
                        activity.finish();
                        activity = null;
                        i--;
                    }
                }

            }
        }
    }

    // 移除一个activity
    public void popOneActivityWithOutFinish(Activity activity) {
        if (activityStack != null && activityStack.size() > 0) {
            if (activity != null) {
                activityStack.remove(new SoftReference(activity));
                activity = null;
            }

        }
    }

//    // 退出所有activity
//    public void finishAllActivity() {
//        if (activityStack != null) {
//            while (activityStack.size() > 0) {
//                Activity activity = getLastActivity();
//                if (activity == null)
//                    break;
//                popOneActivity(activity);
//            }
//        }
//
//    }

    public boolean isActivityRunning(Class<?> activity) {
        if (activityStack != null && activityStack.size() > 0) {
            for (SoftReference<Activity> reference : activityStack) {

                if (reference.get() != null && reference.get().getClass() != null
                        && reference.get().getClass().getSimpleName().equals(activity.getSimpleName())) {
                    return true;
                }
            }
        }

        return false;
    }


    public Activity getActivityForName(Class<?> activity) {
        if (activityStack != null && activityStack.size() > 0) {
            for (SoftReference<Activity> reference : activityStack) {

                if (reference.get() != null && reference.get().getClass() != null
                        && reference.get().getClass().getSimpleName().equals(activity.getSimpleName())) {
                    return reference.get();
                }
            }
        }

        return null;
    }

    public List<SoftReference<Activity>> getActivityStack() {
        return activityStack;
    }
}
